package com.lhgg.examol.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.lhgg.examol.dao.IbaseDao;
import com.lhgg.examol.util.PageUtil;

public abstract class BaseDaoImpl<T, PK extends Serializable> extends
		SqlMapClientDaoSupport implements IbaseDao<T, PK> {
	private Class<T> instance;
	private String ClassName;
	private String simpleClassName;

	public BaseDaoImpl() {
		this.instance = (Class<T>) ((ParameterizedType) this.getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
		this.ClassName = instance.getName().toLowerCase();
		this.simpleClassName = instance.getSimpleName();
	}

	@Override
	public boolean add(T entity) {
		// TODO Auto-generated method stub
		return getSqlMapClientTemplate().update(
				ClassName + "." + "add" + simpleClassName, entity) > 0;
	}

	@Override
	public boolean addBatch(final List<T> list) {
		// TODO Auto-generated method stub
		return (Integer) getSqlMapClientTemplate().execute(
				new SqlMapClientCallback() {
					@Override
					public Object doInSqlMapClient(SqlMapExecutor arg0)
							throws SQLException {
						arg0.startBatch();
						for (T entity : list) {
							arg0.update(ClassName + "." + "add"
									+ simpleClassName, entity);
						}
						return arg0.executeBatch();
					}
				}) == list.size();
	}

	@Override
	public boolean delete(PK pk) {
		// TODO Auto-generated method stub
		return getSqlMapClientTemplate().update(
				ClassName + "." + "delete" + simpleClassName, pk) > 0;
	}

	@Override
	public boolean update(T entity) {
		// TODO Auto-generated method stub
		return getSqlMapClientTemplate().update(
				ClassName + "." + "update" + simpleClassName, entity) > 0;
	}

	@Override
	public List<T> getAll() {
		// TODO Auto-generated method stub
		return (List<T>) getSqlMapClientTemplate().queryForList(
				ClassName + "." + "getAll" + simpleClassName);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return (Integer) getSqlMapClientTemplate().queryForObject(
				ClassName + "." + "getCount" + simpleClassName);
	}

	@Override
	public List<T> getPage(PageUtil<T> pageUtil) {
		// TODO Auto-generated method stub
		return getSqlMapClientTemplate().queryForList(
				ClassName + "." + "getPage" + simpleClassName, pageUtil);
	}

	@Override
	public T getById(PK pk) {
		// TODO Auto-generated method stub
		return (T) getSqlMapClientTemplate().queryForList(
				ClassName + "." + "getById" + simpleClassName, pk);
	}

	@Override
	public int getCount(T entity) {
		// TODO Auto-generated method stub
		return (Integer) getSqlMapClientTemplate().queryForObject(
				ClassName + "." + "getCountByObject" + simpleClassName, entity);
	}

	@Override
	public List<T> getPage(PageUtil<T> pageUtil, T entity) {
		// TODO Auto-generated method stub
		Map map = new HashMap<String, Object>(2);
		map.put("pageUtil", pageUtil);
		map.put("entity", entity);
		return getSqlMapClientTemplate().queryForList(
				ClassName + "." + "getPageByObject" + simpleClassName, map);
	}

	@Override
	public List<T> getAllByObject(T entity) {
		// TODO Auto-generated method stub
		return (List<T>) getSqlMapClientTemplate().queryForList(
				ClassName + "." + "getAllByObject" + simpleClassName, entity);
	}

}
